Week 2: Java Networking: Sockets

 

This week's online activity is about Socket Communications in Java programming. 

 

    Read the All About Sockets lesson in the Custom Networking Trail of the Java Tutorials.  The All About Sockets Lesson is here http://docs.oracle.com/javase/tutorial/networking/sockets/index.html

(Note: the whole Custom Networking Trail is here http://docs.oracle.com/javase/tutorial/networking/TOC.html , but this activity is just the All About Sockets Lesson)

For best results, read, compile and run the EchoServer and the EchoClient whose source code can be downloaded from the page titled "Reading from and Writing to a Socket" by clicking on the links with those names.

 

    Check your understanding of these new concepts with the online Blackboard Quiz for this activity

Question 1	
Which of the following lines of Java code would be run on the server side?	
	ServerSocket serverSocket = new ServerSocket(hostName);
	ServerSocket serverSocket = new ServerSocket(portNumber); (correct)
	ServerSocket serverSocket = new ServerSocket(hostName,portNumber);
	Socket serverSocket = new Socket(portNumber);
	ServerSocket serverSocket = new Socket();

Question 2
Which of the following lines of Java code would be run on the server side to accept a connection from the client?
	Socket clientSocket = new Socket(portNum).accept();
	Socket clientSocket = serverSocket(portNum).accept();
	Socket clientSocket = new Socket().accept();
	ServerSocket clientSocket = new Socket().accept();
	Socket clientSocket = serverSocket.accept(); (correct)

Question 3
In order for a client and a server to establish a socket connection
	The client program needs to be given the port number, and the server program needs to be given both the port number and the client's IP address	
	The server program needs to be given the port number, and the client program needs to be given both the port number and the client's IP address
	The server program needs to be given the port number, and the client program needs to be given both the port number and the server's IP address (correct)
	The client program needs to be given the port number, and the server program needs to be given both the port number and the server's IP address

Question 4
Which statement about Java Networking is true?
	A client uses the ServerSocket class to connect to a server
	A server listens on a port and a client makes a connection to that port (correct)
	A server uses the Socket class to listen on a port
	A server makes a connection on a port and a client listens on a port
	A client uses the ServerSocket class to listen on a port

Question 5
What are the two classes from the java.net package that are used to implement TCP socket communication between Java programs across a network?
	ClientSocket and ServerSocket
	TCP and UDP	
	SocketTCP and SocketUDP
	Socket and ServerSocket (correct)
	ClientSocket and Socket

Question 6
Which of the following lines of Java code would be run on the client side?
	Socket echoSocket = new Socket(hostName, portNumber); (correct)
	ServerSocket echoSocket = new ServerSocket(portNumber);
	ServerSocket echoSocket = new Socket(hostName, portNumber);
	Socket echoSocket = new ServerSocket(hostName, portNumber);
	Socket echoSocket = new Socket(portNumber);